package com.web.portal.utils;

import com.web.portal.pojo.PortalUser;
import io.jsonwebtoken.Claims;

import java.util.HashMap;
import java.util.Map;

public class ClaimsUtils {
    public static final String ID = "id";
    public static final String USER_NAME = "user_name";
    public static final String ROLES = "roles";
    public static final String AVATAR = "avatar";
    public static final String USER_CODE = "user_code";
    public static final String SIGN = "sign";

    public static Map<String, Object> sobUser2Claims(PortalUser portalUser) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(ID, portalUser.getId());
        claims.put(USER_NAME, portalUser.getUserName());
        claims.put(ROLES, portalUser.getRoles());
        claims.put(AVATAR, portalUser.getAvatar());
        claims.put(SIGN, portalUser.getSign());
        claims.put(USER_CODE, portalUser.getUserCode());
        return claims;
    }

    public static PortalUser claims2Claims(Claims claims) {
        PortalUser portalUser = new PortalUser();
        String id = (String) claims.get(ID);
        portalUser.setId(id);
        String userName = (String) claims.get(USER_NAME);
        portalUser.setUserName(userName);
        String roles = (String) claims.get(ROLES);
        portalUser.setRoles(roles);
        String sign = (String) claims.get(SIGN);
        portalUser.setSign(sign);
        String userCode = (String) claims.get(USER_CODE);
        portalUser.setUserCode(userCode);
        return portalUser;
    }
}
